package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bean.pojo.ItripHotel;
import com.bean.vo.hotel.HotCityBySearchHotelVo;
import com.bean.vo.hotel.SearchHotCityVO;
import com.bean.vo.hotel.SearchHotelVO;
import com.service.HotelListControllerService;
import com.util.PageAssistant;
import com.util.ParamUtil;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class HotelListControllerServiceImpl implements HotelListControllerService {

    @Autowired
    private SolrClient solrClient;

    @Override
    public PageAssistant<HotCityBySearchHotelVo> searchItripHotelPage(SearchHotelVO searchHotelVO, PageAssistant<HotCityBySearchHotelVo> pa) throws Exception {
//                 "destination": "北京市",	  //目的地, string, 必填
//                "keywords": "",				//查询关键字, string
//                "hotelLevel": null,			//酒店级别, integer, 1 ~ 5
//                "featureIds": "",			//酒店特色id, string, 每次只能选一个
//            1    "checkInDate": "",			//入住时间, string (date-time)
//            1    "checkOutDate": "",			//退房时间, string (date-time)
//                "tradeAreaIds": null,		//商圈id, string, 每次只能选一个
//            1    "ascSort": null,			//按照某个字段升序, string, 值:isOkCount, avgScore, minPrice, hotelLevel
//            1    "descSort": null,        	//按照某个字段降序, string, 值:isOkCount, avgScore, minPrice, hotelLevel
//             1   "maxPrice": null,			//最高价, double
//             1   "minPrice": null,			//最低价, double
//                "pageNo": null,
//                "pageSize": null


        SolrQuery query = new SolrQuery("*:*");
        StringBuilder term = new StringBuilder();
        /* 添加主查询条件 */
        if (!ParamUtil.isEmpty(searchHotelVO.getKeywords())) {
            String keyword = searchHotelVO.getKeywords().trim();
            //去除空格
            searchHotelVO.setKeywords(keyword);
            //添加条件
            term.append(" keyword:" + keyword);
        }
        //添加条件
        if (!ParamUtil.isEmpty(searchHotelVO.getDestination())) {
            term.append(" or destination:" + searchHotelVO.getDestination());
        }
        if (!ParamUtil.isEmpty(searchHotelVO.getHotelLevel())) {
            term.append(" or hotelLevel:" + searchHotelVO.getHotelLevel());
        }
        if (!ParamUtil.isEmpty(searchHotelVO.getTradeAreaIds())) {
            term.append(" or tradingAreaIds:" + searchHotelVO.getTradeAreaIds());
        }
//        //商圈id
//        if (!ParamUtil.isEmpty(searchHotelVO.getTradeAreaIds())) {
//            query.addFilterQuery("tradingAreaIds:" + searchHotelVO.getTradeAreaIds());
//        }

        query.setQuery(term.toString());

        /* 添加过滤条件 */
        //酒店特色id
        if (!ParamUtil.isEmpty(searchHotelVO.getFeatureIds())) {
            query.addFilterQuery("featureIds:" + searchHotelVO.getFeatureIds());
        }
        //最低价
        if (!ParamUtil.isEmpty(searchHotelVO.getMinPrice())) {
            query.addFilterQuery("minPrice:[" + searchHotelVO.getMinPrice() + " TO *]");
        }
        //最高价
        if (!ParamUtil.isEmpty(searchHotelVO.getMaxPrice())) {
            query.addFilterQuery("maxPrice:[* TO " + searchHotelVO.getMaxPrice() + "]");
        }

        System.out.println("++++日期筛选"+searchHotelVO.getCheckInDate()+" "
        +searchHotelVO.getCheckOutDate());
//        //日期范围
//        //入住
//        if (!ParamUtil.isEmpty(searchHotelVO.getCheckInDate())) {
//            String minLastmodifytime = String.valueOf(searchHotelVO.getCheckInDate());
//            searchHotelVO.setCheckInDate(minLastmodifytime);
//            minLastmodifytime += "T00:00:00Z";
//
//            query.addFilterQuery("lastmodifytime:[\"" + minLastmodifytime + "\" TO *]");
//        }
//        //退房
//        if (!ParamUtil.isEmpty(searchHotelVO.getCheckInDate())) {
//            queryVo.setMaxLastmodifytime(maxLastmodifytime);
//            maxLastmodifytime += "T00:00:00Z";
//            query.addFilterQuery("lastmodifytime:[* TO \"" + maxLastmodifytime + "\"]");
//        }

        //获取总记录数
        //查询, 获取查询响应
        QueryResponse queryResponse2 = solrClient.query(query);
        //通过查询响应, 获取solr的结果集合
        SolrDocumentList results2 = queryResponse2.getResults();

        /* 添加排序 */
        if (!ParamUtil.isEmpty(searchHotelVO.getAscSort())) {
            query.setSort(searchHotelVO.getAscSort(), SolrQuery.ORDER.asc);
        }
        if (!ParamUtil.isEmpty(searchHotelVO.getDescSort())) {
            query.setSort(searchHotelVO.getDescSort(), SolrQuery.ORDER.desc);
        }

        /* 添加分页 */
//        //起始序号
//        query.setStart(pa.getStart());
//        //取几条数据
//        query.setRows(5);

        //查询, 获取查询响应
        QueryResponse queryResponse = solrClient.query(query);
        //通过查询响应, 获取solr的结果集合
        SolrDocumentList results = queryResponse.getResults();
        System.out.println("solr长度"+results.size());
        //声明集合
        List<HotCityBySearchHotelVo> hotCityBySearchHotelVo = new ArrayList<>();
        //遍历solr的结果集合, 将solr的document对象转换成java的vo对象, 放入集合hotCityBySearchHotelVo
        for (SolrDocument sd : results) {

            String jsonTemp = JSONObject.toJSONStringWithDateFormat(sd, "yyyy-MM-dd HH:mm:ss");

            hotCityBySearchHotelVo.add(JSONObject.parseObject(jsonTemp, HotCityBySearchHotelVo.class));
        }

        System.out.println("solr测试"+hotCityBySearchHotelVo);

        //分页数据
        pa.setPageSize(5);
        pa.setTotal(results2.size());
        pa.setCurPage(searchHotelVO.getPageNo());
        pa.setRows(hotCityBySearchHotelVo);
        return pa;
    }

    @Override
    public List<HotCityBySearchHotelVo> searchItripHotelListByHotCity(SearchHotCityVO searchHotCityVO) throws Exception {
        SolrQuery query = new SolrQuery("*:*");

        StringBuilder term=new StringBuilder();

        term.append("cityId:"+searchHotCityVO.getCityId());

        query.setQuery(term.toString());

        QueryResponse response = solrClient.query(query);

        SolrDocumentList results = response.getResults();

        //酒店集合
        List<HotCityBySearchHotelVo> itripHotelList = new ArrayList<>();

        //遍历solr的结果集合
        for (SolrDocument doc : results) {
            //将一个solr的查询结果, 转换成json字符串
            String tempJson = JSONObject.toJSONStringWithDateFormat(doc, "yyyy-MM-dd HH:mm:ss");
            //将json转换成ItripHotel对象
            HotCityBySearchHotelVo ih = JSONObject.parseObject(tempJson, HotCityBySearchHotelVo.class);
            //添加到userList
            itripHotelList.add(ih);
        }

        for (HotCityBySearchHotelVo i:
                itripHotelList) {
            System.out.println(i);
        }

        return itripHotelList;
    }
}
























